package com.whmskj.xjlhbc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whmskj.xjlhbc.domain.Bctj14ProductionMonthlyCompletion;
import com.whmskj.xjlhbc.dto.MonthlyProductionQueryDto;
import com.whmskj.xjlhbc.mapper.Bctj14ProductionMonthlyCompletionMapper;
import com.whmskj.xjlhbc.service.IBctj14ProductionMonthlyCompletionService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * BC-TJ-14产量月完成情况Service实现类
 */
@Service
public class Bctj14ProductionMonthlyCompletionServiceImpl
        extends ServiceImpl<Bctj14ProductionMonthlyCompletionMapper, Bctj14ProductionMonthlyCompletion>
        implements IBctj14ProductionMonthlyCompletionService {

    @Override
    public List<Map<String, Object>> queryList(MonthlyProductionQueryDto queryDto) {
        LambdaQueryWrapper<Bctj14ProductionMonthlyCompletion> wrapper = new LambdaQueryWrapper<>();

        if (queryDto.getYear() != null) {
            wrapper.eq(Bctj14ProductionMonthlyCompletion::getYear, queryDto.getYear());
        }
        if (queryDto.getMonth() != null) {
            wrapper.eq(Bctj14ProductionMonthlyCompletion::getMonth, queryDto.getMonth());
        }
        if (StringUtils.hasText(queryDto.getStartDate())) {
            wrapper.ge(Bctj14ProductionMonthlyCompletion::getOtherOrganiser, queryDto.getStartDate());
        }
        if (StringUtils.hasText(queryDto.getEndDate())) {
            wrapper.le(Bctj14ProductionMonthlyCompletion::getEndDate, queryDto.getEndDate());
        }

        wrapper.orderByDesc(Bctj14ProductionMonthlyCompletion::getYear)
               .orderByDesc(Bctj14ProductionMonthlyCompletion::getMonth);

        List<Bctj14ProductionMonthlyCompletion> records = list(wrapper);
        List<Map<String, Object>> result = new ArrayList<>();

        for (Bctj14ProductionMonthlyCompletion record : records) {
            Map<String, Object> item = new HashMap<>();
            item.put("id", record.getId());
            item.put("year", record.getYear());
            item.put("month", record.getMonth());
            item.put("monthDisplay", record.getMonthDisplay());
            item.put("otherOrganiser", record.getOtherOrganiser());
            item.put("endDate", record.getEndDate());
            item.put("zjjhcl", record.getZjjhcl());
            item.put("zjwccl", record.getZjwccl());
            item.put("zjwcbl", record.getZjwcbl());
            item.put("zjxc", record.getZjxc());
            item.put("jsjhcl", record.getJsjhcl());
            item.put("jswccl", record.getJswccl());
            item.put("jswcbl", record.getJswcbl());
            item.put("jsxc", record.getJsxc());
            item.put("zbsjjhcl", record.getZbsjjhcl());
            item.put("zbsjwccl", record.getZbsjwccl());
            item.put("zbsjwcbl", record.getZbsjwcbl());
            item.put("zbsjxc", record.getZbsjxc());
            item.put("zbzbjhcl", record.getZbzbjhcl());
            item.put("zbzbwccl", record.getZbzbwccl());
            item.put("zbzbwcbl", record.getZbzbwcbl());
            item.put("zbzbxc", record.getZbzbxc());
            item.put("zbzbwcx", record.getZbzbwcxs());
            item.put("updateTime", record.getUpdateTime());
            result.add(item);
        }

        return result;
    }

    @Override
    public Map<String, Object> getMonthlyReport(MonthlyProductionQueryDto queryDto) {
        Map<String, Object> report = new HashMap<>();

        LambdaQueryWrapper<Bctj14ProductionMonthlyCompletion> wrapper = new LambdaQueryWrapper<>();

        // 如果指定了起止日期，按日期范围查询
        if (StringUtils.hasText(queryDto.getStartDate()) && StringUtils.hasText(queryDto.getEndDate())) {
            wrapper.ge(Bctj14ProductionMonthlyCompletion::getOtherOrganiser, queryDto.getStartDate());
            wrapper.le(Bctj14ProductionMonthlyCompletion::getEndDate, queryDto.getEndDate());
        } else {
            // 否则按年月查询
            if (queryDto.getYear() != null) {
                wrapper.eq(Bctj14ProductionMonthlyCompletion::getYear, queryDto.getYear());
            }
            if (queryDto.getMonth() != null) {
                wrapper.eq(Bctj14ProductionMonthlyCompletion::getMonth, queryDto.getMonth());
            }
        }

        List<Bctj14ProductionMonthlyCompletion> records = list(wrapper);

        if (!records.isEmpty()) {
            Bctj14ProductionMonthlyCompletion record = records.get(0);

            report.put("reportYear", record.getYear());
            report.put("reportMonth", record.getMonth());
            report.put("startDate", record.getOtherOrganiser());
            report.put("endDate", record.getEndDate());

            // 整经产量数据
            Map<String, Object> warping = new HashMap<>();
            warping.put("planned", record.getZjjhcl());
            warping.put("actual", record.getZjwccl());
            warping.put("completionRatio", calculateCompletionRatio(record.getZjwccl(), record.getZjjhcl()));
            warping.put("difference", calculateDifference(record.getZjwccl(), record.getZjjhcl()));
            report.put("warping", warping);

            // 浆纱产量数据
            Map<String, Object> sizing = new HashMap<>();
            sizing.put("planned", record.getJsjhcl());
            sizing.put("actual", record.getJswccl());
            sizing.put("completionRatio", calculateCompletionRatio(record.getJswccl(), record.getJsjhcl()));
            sizing.put("difference", calculateDifference(record.getJswccl(), record.getJsjhcl()));
            report.put("sizing", sizing);

            //整理产量数据
            Map<String, Object> zlMap = new HashMap<>();
            zlMap.put("planned", record.getZljhcl());
            zlMap.put("actual", record.getZlwccl());
            zlMap.put("completionRatio", calculateCompletionRatio(record.getZlwccl(), record.getZljhcl()));
            zlMap.put("difference", calculateDifference(record.getZlwccl(), record.getZljhcl()));
            report.put("zl", zlMap);

            // 织布产量数据
            Map<String, Object> weaving = new HashMap<>();

            // 织布实际产量
            Map<String, Object> actualProduction = new HashMap<>();
            actualProduction.put("planned", record.getZbsjjhcl());
            actualProduction.put("actual", record.getZbsjwccl());
            actualProduction.put("completionRatio", calculateCompletionRatio(record.getZbsjwccl(), record.getZbsjjhcl()));
            actualProduction.put("difference", calculateDifference(record.getZbsjwccl(), record.getZbsjjhcl()));
            weaving.put("actualProduction", actualProduction);

            // 织布折标产量
            Map<String, Object> standardProduction = new HashMap<>();
            standardProduction.put("planned", record.getZbzbjhcl());
            standardProduction.put("actual", record.getZbzbwccl());
            standardProduction.put("completionRatio", calculateCompletionRatio(record.getZbzbwccl(), record.getZbzbjhcl()));
            standardProduction.put("difference", calculateDifference(record.getZbzbwccl(), record.getZbzbjhcl()));
            weaving.put("standardProduction", standardProduction);
            weaving.put("standardCompletionFactor", calculateCompletionRatio(record.getZbzbwccl(), record.getZbsjwccl()));
            report.put("weaving", weaving);
        }

        return report;
    }

    /**
     * 计算相差值
     */
    private BigDecimal calculateDifference(BigDecimal actual, BigDecimal planned) {
        if (actual == null || planned == null) {
            return BigDecimal.ZERO;
        }
        return actual.subtract(planned);
    }

    /**
     * 计算完成率 (完成产量 / 计划产量 * 100)
     */
    private BigDecimal calculateCompletionRatio(BigDecimal actual, BigDecimal planned) {
        if (actual == null || planned == null || planned.compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO.setScale(1);
        }
        return actual
                .divide(planned, 4, java.math.RoundingMode.HALF_UP) // 提高中间计算精度
                .multiply(new BigDecimal("100"))
                .setScale(1, java.math.RoundingMode.HALF_UP); // 最终保留一位小数
    }
}
